import VueRouter from 'vue-router'
import Vue from 'vue'
import Login from '@/views/login'
import Layout from '@/views/home/Layout.vue'
import auth from '@/utils/auth'
Vue.use(VueRouter)

// 初始化
const router = new VueRouter({
  // 路由规则
  routes: [
    { path: '/login', component: Login },
    { path: '/', component: Layout }
  ]
})

router.beforeEach((to, from, next) => {
  // to: 将要进入的目标
  // from: 当前导航正要离开的路由
  // next: 继续路由
  // next() 放行
  // next(false) 中断路由
  // next('/login') 跳转到登录页面
  // next(to.path) 跳转到当前页面
  const token = auth.getToken()
  if (to.path !== '/login' && !token) {
    return next('/login')
  }
  next()
})
export default router
